import pymongo
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from pyquery import PyQuery as pq
from config import *
from urllib.parse import quote
import time
import random

# browser = webdriver.Chrome()
# browser = webdriver.PhantomJS(service_args=SERVICE_ARGS)

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
# chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)

wait = WebDriverWait(browser, 10)
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]


def move_captcha(button):
    ActionChains(browser).click_and_hold(button).perform()
    pix = 258
    li = []
    # li = [16, 10, 47, 45, 22, 24, 1, 4, 48, 38, 3]
    # for offset in li:
    #     ActionChains(browser).move_by_offset(offset, 0).perform()
    while pix != 0:
        ActionChains(browser).move_by_offset(258, 0).perform()
        offset = random.randint(1, 80)
        if pix < offset:
            offset = pix
        ActionChains(browser).move_by_offset(offset, 0).perform()
        li.append(offset)
        pix -= offset
    ActionChains(browser).release(button).perform()
    return li


def move_to_page(page):
    input = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, '#mainsrp-pager div.form > input')))
    submit = wait.until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, '#mainsrp-pager div.form > span.btn.J_Submit')))
    input.clear()
    input.send_keys(page)
    submit.click()


def index_page(page):
    """
    抓取索引页
    :param page: 页码
    """
    print('正在爬取第', page, '页')
    try:
        url = 'https://s.taobao.com/search?q=' + quote(KEYWORD)
        browser.get(url)
        while browser.title == '淘宝网 - 淘！我喜欢':
            try:
                browser.find_element_by_id('J_Quick2Static').click()
            except Exception:
                pass
            ele_username = browser.find_element_by_id('TPL_username_1')
            if not ele_username.get_attribute('value'):
                ele_username.send_keys('****用户名****')
            browser.find_element_by_id('TPL_password_1').send_keys('****密码****')
            button = browser.find_element_by_id('nc_1_n1z')
            if button.size['height'] != 0:
                move_captcha(button)
            browser.find_element_by_id('J_SubmitStatic').click()
            time.sleep(2)
        if page > 1:
            move_to_page(page)
            time.sleep(1)
        try:
            browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="J_sufei"]/iframe'))
            browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
            button = browser.find_element_by_id('nc_1_n1z')
            li = move_captcha(button)
            time.sleep(1.5)

            while True:
                try:
                    browser.find_element_by_xpath('//*[@id="nocaptcha"]/div/span/a').click()
                    li = move_captcha(browser.find_element_by_id('nc_1_n1z'))
                    time.sleep(1.5)
                except Exception:
                    print(li)
                    break

            browser.switch_to.default_content()
            move_to_page(page)
        except Exception:
            pass
        wait.until(
            EC.text_to_be_present_in_element((By.CSS_SELECTOR, '#mainsrp-pager li.item.active > span'), str(page)))
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.m-itemlist .items .item')))
        get_products()
    except TimeoutException:
        index_page(page)


def get_products():
    """
    提取商品数据
    """
    html = browser.page_source
    doc = pq(html)
    items = doc('#mainsrp-itemlist .items .item').items()
    for item in items:
        product = {
            'image': item.find('.pic .img').attr('data-src'),
            'price': item.find('.price').text(),
            'deal': item.find('.deal-cnt').text(),
            'title': item.find('.title').text(),
            'shop': item.find('.shop').text(),
            'location': item.find('.location').text()
        }
        print(product)
        save_to_mongo(product)


def save_to_mongo(result):
    """
    保存至MongoDB
    :param result: 结果
    """
    try:
        if db[MONGO_COLLECTION].insert(result):
            print('存储到MongoDB成功')
    except Exception:
        print('存储到MongoDB失败')


def main():
    """
    遍历每一页
    """
    for i in range(1, MAX_PAGE + 1):
        index_page(i)
    browser.close()


if __name__ == '__main__':
    main()
